package com.myBatisTest.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myBatisTest.config.MyBloomFilter;
import com.myBatisTest.entity.Employee;
import com.myBatisTest.service.DmployeeService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DmployeeController {
    @Autowired
    DmployeeService dmployeeService;
    @Autowired
    MyBloomFilter myBloomFilter;

    @RequestMapping("getAll")
    public List<Employee> getAll(@RequestBody Employee employee){
        LambdaQueryWrapper<Employee> like = Wrappers.lambdaQuery(Employee.class).
                like(StringUtils.hasText(employee.getAccount()), Employee::getAccount, employee.getAccount());
        return dmployeeService.list(like);
    }

    @RequestMapping("delete")
    public boolean delete(Integer id){
        return dmployeeService.removeById(id);
    }

    @RequestMapping("update")
    public Employee update(@RequestBody Employee employee){
        return dmployeeService.saveOrUpdateWithCache(employee);
    }

    @RequestMapping("getId")
    public Employee getId(Integer id){
        if(myBloomFilter.bloomFilter.mightContain(id)){
            System.out.println("id存在数据库中");
            return dmployeeService.getById(id);
        }else {
            System.out.println("id不存在数据库中");
            return null;
        }
    }

    @RequestMapping("/insert")
    public Employee insert(@RequestBody Employee employee){
        return dmployeeService.saveWithCache(employee);
    }

    @RequestMapping("/page")
    public IPage<Employee> page(@RequestParam(defaultValue = "1")long pageNo,@RequestBody Employee employee){
        LambdaQueryWrapper<Employee> like = Wrappers.lambdaQuery(Employee.class).
                like(StringUtils.hasText(employee.getAccount()), Employee::getAccount, employee.getAccount());
        return dmployeeService.page(Page.of(pageNo,2),like);
    }
}